<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">添加</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column label="批次号" prop="batchCode" show-overflow-tooltip />
      <el-table-column label="间隔时间(天)" min-width="100" prop="intervalTime" show-overflow-tooltip />
      <el-table-column label="饲料名称" prop="feedName" show-overflow-tooltip />
      <el-table-column label="数量(单只鸡)" min-width="100" prop="feedPer" show-overflow-tooltip />
      <el-table-column align="center" fixed="right" label="操作" width="90">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <feed-plan-desktop-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { API_feedPlanList, API_feedPlanDelete } from '@/api/feedPlan'
  import FeedPlanDesktopEdit from './components/FeedPlanDesktopEdit'
  export default {
    name: 'FeedPlan',
    components: { FeedPlanDesktopEdit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
        },
        timeOutID: null,
      }
    },
    computed: {
      ...mapGetters({
        device: 'settings/device',
      }),
    },
    created() {
      this.fetchData()
    },
    mounted() {
      const route = this.$route
      const { params } = route
      if (params.refresh) {
        this.fetchData()
      }
    },
    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      handleEdit(row) {
        if (this.device === 'mobile') {
          return this.$router.push({
            name: 'FeedPlanMobileEdit',
            params: { ...row },
          })
        }
        if (row.code) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.code) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { message } = await API_feedPlanDelete({ code: row.code })
            this.$baseMessage(message, 'success')
            this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await API_feedPlanList(this.queryForm)
        this.list = data.list
        this.total = data.count
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
